package com.aegean.socket.apacheMINA.handler;

import org.apache.log4j.Logger;
import org.apache.mina.core.service.IoHandler;
import org.apache.mina.core.service.IoHandlerAdapter;
import org.apache.mina.core.session.IdleStatus;
import org.apache.mina.core.session.IoSession;

public class ServiceMessageHandler extends IoHandlerAdapter {
	private static Logger log = Logger.getLogger(ServiceMessageHandler.class);

	@Override
	public void exceptionCaught(IoSession session, Throwable cause)
			throws Exception {
		// TODO Auto-generated method stub
		if (log.isInfoEnabled()) {
			log.warn("客户端" + getClass().getName()
					+ ":出现断网现象", cause);
		}
	}

	@Override
	public void messageReceived(IoSession session, Object message)
			throws Exception {
		// TODO Auto-generated method stub
		if(message instanceof String){
			log.info("收到客户端消息:"+session.getLocalAddress()+"-"+session.getRemoteAddress()+"-"+message.toString());
		}else{
			log.error("消息不是String类型！");
			session.close(false);
		}
		
	}

	@Override
	public void messageSent(IoSession session, Object message) throws Exception {
		// TODO Auto-generated method stub
		log.info("服务器发送消息成功！");
	}

	@Override
	public void sessionClosed(IoSession session) throws Exception {
		// TODO Auto-generated method stub
		log.info("服务器关闭客户端session！");
	}

	@Override
	public void sessionCreated(IoSession session) throws Exception {
		// TODO Auto-generated method stub
		log.info("服务器与客户端创建连接！");
	}

	@Override
	public void sessionIdle(IoSession session, IdleStatus status)
			throws Exception {
		// TODO Auto-generated method stub
		log.info("服务器发现客户端session出现闲置现象！");
	}

	@Override
	public void sessionOpened(IoSession session) throws Exception {
		// TODO Auto-generated method stub
		log.info("服务器与客户端打开连接！");
	}

}
